package com.kang.commons.util;

import com.kang.bean.Email;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @program: photo_fawu
 * @description: 发送邮件
 * @author: K.faWu
 * @create: 2021-11-12 16:41
 **/

@Component
public class EmailUtil {

    static String user = "k13045056919@163.com";//用户名
    static String pass = "QWPGBOMAMRHYXQHN";//密码
    static Session session;
    static Message message;
    static {
        Properties prop = new Properties();
        prop.put("mail.transport.protocol", "smtp");//协议
        prop.put("mail.smtp.port", 587);//端口
        prop.put("mail.smtp.host", "smtp.163.com");//地址
        prop.put("mail.smtp.auth", "true");//认证
        prop.put("mail.debug", "true");//测试
        prop.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        // 得到回话对象
        session = Session.getInstance(prop);
        // 获取邮件对象
        message = new MimeMessage(session);
    }
    static String code = "000000";

     public void sendText(Email email) throws Exception {
         // 设置发件人邮箱地址
         message.setFrom(new InternetAddress(user));
         // 设置收件人邮箱地址
         //message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
         message.setRecipient(Message.RecipientType.TO, new InternetAddress(email.getReceiver()));
         // 设置邮件标题
         message.setSubject(email.getSubject());
         // 设置邮件内容
         message.setText(email.getContent());
         // 得到邮差对象
         Transport transport = session.getTransport();
         // 连接自己的邮箱账户
         transport.connect(user, pass);// 密码为邮箱开通的stmp服务后得到的客户端授权码
         // 发送邮件
         transport.sendMessage(message, message.getAllRecipients());
         transport.close();
     }

     public static String sendCode(String email) {
         code = (int) ((Math.random() * 9 + 1) * 100000) + "";
         // 设置发件人邮箱地址
         try {
             message.setFrom(new InternetAddress(user));

             // 设置收件人邮箱地址
             //message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
             message.setRecipient(Message.RecipientType.TO, new InternetAddress( email ));
             // 设置邮件标题
             message.setSubject("【法兀IT】");
             // 设置邮件内容
             message.setText(
                     "您好，\n" +
                     "您正在通过邮箱注册【法兀IT】账号，验证码为："+code+"，有效时间为 5 分钟。\n" +
                             "若非您本人操作请忽略此封邮件，祝您生活愉快！");
             // 得到邮差对象
             Transport transport = session.getTransport();
             // 连接自己的邮箱账户
             transport.connect(user, pass);// 密码为邮箱开通的stmp服务后得到的客户端授权码
             // 发送邮件
             transport.sendMessage(message, message.getAllRecipients());
             transport.close();
         } catch (MessagingException e) {
             e.printStackTrace();
         }
         return  code;
     }
}
